package common.util;

import com.aspose.words.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 葛成瑞
 * @Date: 2024/5/8 16:29
 */
@Service
@Slf4j
public class WordUtil {

    @Resource
    ExtractUtil extractUtil;

    public static List<String> wordToTextStatic(String path) {
        log.error("word 解析开始");
        List<String> res = new ArrayList<>();
        try {
            try {
                ExtractUtil.getLicenseStatic();
            } catch (Exception e) {

            }
            String filePath = path;
            Document doc = new Document(filePath);
            SectionCollection sections = doc.getSections();
            for (Section section : sections) {
                ParagraphCollection paragraphs = section.getBody().getParagraphs();
                for (Paragraph paragraph : paragraphs) {
                    if (StringUtil.isNotNull(paragraph.getText())) {
                        res.add(paragraph.getText());
                    }
                }
            }
        } catch (Exception e) {
            log.error("word 解析异常");
        }
        log.info("word 解析出来的数量+" + res.size());
        return res;
    }

    public List<String> wordToText(String path) {
        log.error("word 解析开始");
        List<String> res = new ArrayList<>();
        try {
            extractUtil.getLicense();
            String filePath = path;
            Document doc = new Document(filePath);
            SectionCollection sections = doc.getSections();
            for (Section section : sections) {
                ParagraphCollection paragraphs = section.getBody().getParagraphs();
                for (Paragraph paragraph : paragraphs) {
                    res.add(paragraph.getText());
                }
            }
        } catch (Exception e) {
            log.error("word 解析异常");
        }
        log.info("word 解析出来的数量+" + res.size());
        return res;
    }
}
